package com.sec.android.app.myfiles.shadow.custom;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.UnzipParameters;
import net.lingala.zip4j.progress.ProgressMonitor;

import org.powermock.api.mockito.PowerMockito;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static org.powermock.api.mockito.PowerMockito.when;

/**
 * Created by jaebae on 2016-07-11.
 */
@Implements(ZipFile.class)
public class ShadowZipFile {
    public static final int TEST_ZIP_ITEM_CNT = 16;
    private String mFilePath;

    public void __constructor__(File file) {
        mFilePath = file.getAbsolutePath();
    }

    @Implementation
    public boolean isValidZipFile() {
        boolean ret = !mFilePath.toLowerCase(Locale.getDefault()).contains("invalid");
        return ret;
    }

    @Implementation
    public void setRunInThread(boolean runInThread) {

    }

    public List getFileHeaders() throws ZipException {
        ArrayList<FileHeader> ret = new ArrayList<>();

        for (int i = 0; i < TEST_ZIP_ITEM_CNT; i++) {
            ret.add(createHeader(i));
        }

        return ret;
    }


    private FileHeader createHeader(int index) {
        FileHeader ret = PowerMockito.mock(FileHeader.class);

        String innerName = String.format(Locale.getDefault(), "inner_zip %d.dat", index);
        when(ret.getFileName()).thenReturn(innerName);
        when(ret.isDirectory()).thenReturn(false);

        return ret;
    }

    @Implementation
    public void extractFile(FileHeader fileHeader, String destPath, UnzipParameters unzipParameters, String newFileName) throws ZipException {
    }

    public ProgressMonitor getProgressMonitor() {
        ProgressMonitor ret = PowerMockito.mock(ProgressMonitor.class);

        when(ret.getResult()).thenReturn(ProgressMonitor.RESULT_SUCCESS);
        when(ret.getState()).thenReturn(ProgressMonitor.STATE_READY);
        when(ret.getException()).thenReturn(null);


        return ret;
    }
}
